﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Utem.Server.UserContols
{
    public partial class ScorePanel : UserControl
    {
        private const int TOP = 1;
        private const int WIDTH = 25;
        private const int HEIGHT = 40;

        private const int LEFT0 = 10;
        private const int LEFT1 = 100;
        private const int LEFT2 = 185;
        private const int LEFT3 = 270;
        private const int LEFT4 = 355;
        private const int LEFT5 = 440;
        private const int LEFT6 = 525;
        private const int LEFT7 = 610;
        private const int LEFT8 = 695;
        private readonly int[] LEFT = { LEFT0, LEFT1, LEFT2, LEFT3, LEFT4, LEFT5, LEFT6, LEFT7, LEFT8 };

        private readonly IntWrapper[] players;

        private readonly List<IntWrapper>[] scores;

        private readonly Image[] images;

        public ScorePanel()
        {
            InitializeComponent();

            // Initial player location.
            players = new IntWrapper[6];
            for (var i = 0; i < 6; i++)
            {
                players[i] = new IntWrapper { Value = LEFT0 + 10 * i };
            }

            // Initial score lists.
            scores = new List<IntWrapper>[9];
            for (var i = 0; i < 9; i++)
            {
                scores[i] = new List<IntWrapper>();
            }

            // Add all players to score 0.
            for (var i = 0; i < 6; i++)
            {
                scores[0].Add(players[i]);
            }

            // Initial player images.
            images = new Image[6];
            images[0] = Properties.Resources.player1;
            images[1] = Properties.Resources.player2;
            images[2] = Properties.Resources.player3;
            images[3] = Properties.Resources.player4;
            images[4] = Properties.Resources.player5;
            images[5] = Properties.Resources.player6;
            
        }

        public void SetPoint(int pos, int point)
        {
            if (pos < 0 || pos > 5) return;
            if (point < 0 || point > 8) return;
            var player = players[pos];

            for (var i = 0; i < 9; i++)
            {
                if (!scores[i].Contains(player)) continue;

                if (i == point) return;

                // Remove in the list of last score.
                scores[i].Remove(player);

                // Re-arrange.
                for (var j = 0; j < scores[i].Count; j++)
                {
                    scores[i][j].Value = 10 * (scores[i].Count - j - 1) + LEFT[i];
                }

                scores[point].Add(player);
                // Re-arrange.
                for (var j = 0; j < scores[point].Count; j++)
                {
                    scores[point][j].Value = 10 * (scores[point].Count - j - 1) + LEFT[point];
                }

                Refresh();
                return;
            }

        }

        public void IncreasePoint(int pos)
        {
            if (pos < 0 || pos > 5) return;
            var player = players[pos];

            for (var i = 0; i < 8; i++)
            {
                if (!scores[i].Contains(player)) continue;

                // Remove in the list of last score.
                scores[i].Remove(player);

                // Re-arrange.
                for (var j = 0; j < scores[i].Count; j++)
                {
                    scores[i][j].Value = 10 * (scores[i].Count - j - 1) + LEFT[i];
                }


                // Prepare a place for player.
                for (var j = 0; j < scores[i + 1].Count; j++)
                {
                    scores[i + 1][j].Value += 10;
                }

                // Change player location.
                player.Value = LEFT[i + 1];
                scores[i + 1].Add(player);

                Refresh();
                return;
            }
        }

        public void DecreasePoint(int pos)
        {
            if (pos < 0 || pos > 5) return;
            var player = players[pos];

            for (var i = 1; i < 9; i++)
            {
                if (!scores[i].Contains(player)) continue;

                // Remove in the list of last score.
                scores[i].Remove(player);

                // Re-arrange.
                for (var j = 0; j < scores[i].Count; j++)
                {
                    scores[i][j].Value = 10 * (scores[i].Count - j - 1) + LEFT[i];
                }


                // Change player location.
                player.Value = LEFT[i - 1] + 10 * scores[i - 1].Count;
                scores[i - 1].Add(player);

                Refresh();
                return;
            }
        }

        public void Reset()
        {
            for (var i = 0; i < 9; i++)
            {
                scores[i].Clear();
            }

            for (var i = 0; i < 6; i++)
            {
                players[i].Value = LEFT0 + 10 * i;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Size = new Size(800, 75);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var scoreBar = Properties.Resources.scrorebar;
            e.Graphics.DrawImage(scoreBar, 11, 35, 778, 33);

            for (var i = 0; i < 6; i++)
            {
                e.Graphics.DrawImage(images[i], players[i].Value, TOP, WIDTH, HEIGHT);
            }
        }

        private class IntWrapper
        {
            public int Value { get; set; }
        }
    }
}
